package user

import (
	"beego/testApp/common/db"
	"beego/testApp/common/tool"
	"fmt"
	"database/sql"
	_ "github.com/mattn/go-sqlite3"
	"strconv"
	"math"
	"time"
	//"encoding/json"
)

type Admin struct {
    Uid  int     
	Name string   
	Created string 
}

func CkAdminUserExist() bool {

	db := db.Opendb("admin")
	if(db == nil){

		tool.Pic("CkAdminUserExist ","open db fail")
		return false
	}

	sql := "select * from admin"

	rows,err := db.Query(sql);

	defer db.Close()

	if(err!=nil){
		tool.Pic("CkAdminUserExist db query fial",err)
	   return false
	}
   
	

	for rows.Next() {

		defer rows.Close()

		return true
	} 
	

	
    return false 

}

func Adduser(username  string, pass string ) bool {
	
	db := db.Opendb("admin")
	if(db == nil){
		tool.Pic("Adduser","open db fail")
		return false
	}
	
	sql := "insert into admin(username,pass,created) values(?,?,?)";

	smt,err := db.Prepare(sql)

	defer db.Close()

	if(err!=nil){

		tool.Pic("Adduser prepare fail",err)
		return false
	 }
	
	 now := time.Now(); 

	 res,err := smt.Exec(username,pass,now)

	 defer smt.Close()

	 if(err!=nil){
		tool.Pic("deluser smt exec fail",err)
		return false
	}

	affect, err := res.RowsAffected()

	//defer res.Close()

	if(err!=nil){
		tool.Pic("Adduser get row afffect fail",err)
		return false
	}


	tool.Pic("deluser success affected rows",affect)

	
	
	return true
     

}

func DelUser(id int) bool {
	db := db.Opendb("admin")
	if(db == nil){
		tool.Pic("deluser","open db fail")
		return false
	}

	//sql := "delete from admin where uid = " + strconv.Itoa(id)

	smt,err := db.Prepare("delete from admin where uid=?")

	defer db.Close()

	if(err!=nil){

	   tool.Pic("deluser","prepare fail")
	   return false

	}

	res,err := smt.Exec(id)

	defer smt.Close()

	if(err!=nil){
		tool.Pic("deluser smt exec fail",err)
		return false
	}

	affect, err := res.RowsAffected()

	if(err!=nil){
		tool.Pic("deluser","get row afffect fail")
		return false
	}


	tool.Pic("deluser success affected rows",affect)

	
	
	return true
}

func Page(pagenum int) (float64) {
   db := db.Opendb("admin")

   if(db==nil){
	tool.Pic("can not open admin db","db")
	return 0
   }
   
   sql := "select count(*) as count from admin"

   rows,err := db.Query(sql)

   defer db.Close()

   if(err!=nil){
	   return 0
   }

   var count int

   for rows.Next(){ 
	 rows.Scan(&count)
   }

   defer rows.Close()
   
   fmt.Println(count)

   floatcount := tool.IntoFloat(count)
   floatpagenum := tool.IntoFloat(pagenum) 

   pagecountflaot := floatcount/floatpagenum;

   pagecountflaot = math.Ceil(pagecountflaot)

  // pagecountint := tool.XtoInt(pagecountflaot)

   

   return pagecountflaot
}

func List(page int ,pagenum int) []Admin {

	var res []Admin
//	var re Admin

	db := db.Opendb("admin");
	
	if(db==nil){
	   tool.Pic("can not open admin db","db")
       return nil 
	}

	start := (page-1)*pagenum;  

	tool.Pic("start",start)
	tool.Pic("pagenum",pagenum)

	sql := "select uid,username,created from admin limit "+ strconv.Itoa(start)+" , "+ strconv.Itoa(pagenum);

	
	tool.Pic("sql",sql)

	rows,err := db.Query(sql);
	
	defer db.Close()
	
	if(err!=nil){
		return nil
	}

	for rows.Next(){
	    var uid int 
		var admin string
		var created string

		rows.Scan(&uid,&admin,&created)
        //re.Uid  = uid
		//re.Name = admin
		//re.Created = created

		re :=Admin{Uid:uid,Name:admin,Created:created}


        
		fmt.Println(re);

		//rejson,_ := json.Marshal(re)

		//rejson := tool.Tojson(re)

		//tool.Pic("temp data re json test  ",rejson)
        // res[] =re;
	    res = append(res,re)
	}

	defer rows.Close()
	
	return res
}

func CkUserExist(username string ) bool {

	db  := db.Opendb("admin");
	sql :="select * from admin where username ='"+username +"'";
	rows,err := db.Query(sql);

	defer db.Close()

	if(err!=nil){
	 return false
	}

	for rows.Next() {

		defer rows.Close()

		return true
	} 

	

    return false 
}

func Login(username string,pass string) bool{

   
	
	db, err :=  sql.Open("sqlite3", "./db/admin.db")
	if(err!=nil){
		
		fmt.Println("create or accesss admin db fail");
		return false

	}else{
		
		fmt.Println("create or accesss admin db success");
	}
	
	sql_table := `CREATE TABLE IF NOT EXISTS admin(
		uid INTEGER PRIMARY KEY AUTOINCREMENT,
		username VARCHAR(32) NULL,
		pass VARCHAR(32) NULL,
		created DATE NULL
	);`

   db.Exec(sql_table)

   defer db.Close()

   rows, err := db.Query("select * from admin where username = '"+ username +"' and pass ='"+pass+"'")
   

   

   if(err!=nil){
	   fmt.Println("access admin table fail ");
	   return false;
   }

   for rows.Next(){

	defer rows.Close()

	return true;
	}
    return false
	
}